﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：YM温度特性 上海永铭电子股份有限公司
    /// </summary>
    public class R109 : BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;
            int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            for (int i = 0; i < 1; i++)
            {
                try
                {
                    if (sheet.GetRow(8).GetCell(headColumnsIndex + 1) != null)
                        sheet.GetRow(8).GetCell(headColumnsIndex + 1).SetCellValue(head.Series);
                    sheet.GetRow(8).GetCell(headColumnsIndex + 2).SetCellValue(head.Cap);
                    sheet.GetRow(8).GetCell(headColumnsIndex + 4).SetCellValue(head.Voltage);
                    sheet.GetRow(8).GetCell(headColumnsIndex + 7).SetCellValue(head.GetD());
                    sheet.GetRow(8).GetCell(headColumnsIndex + 9).SetCellValue(head.GetL());

                    sheet.GetRow(9).GetCell(headColumnsIndex + 1).SetCellValue(head.LotNo);

                    sheet.GetRow(11).GetCell(headColumnsIndex + 1).SetCellValue(head.FoilP);
                    sheet.GetRow(12).GetCell(headColumnsIndex + 1).SetCellValue(head.FoilN);
                    sheet.GetRow(13).GetCell(headColumnsIndex + 1).SetCellValue(head.Rubber);
                    sheet.GetRow(14).GetCell(headColumnsIndex + 1).SetCellValue(head.LeadWire);
                    sheet.GetRow(11).GetCell(headColumnsIndex + 7).SetCellValue(head.Eps);
                    sheet.GetRow(12).GetCell(headColumnsIndex + 7).SetCellValue(head.Paste);
                    sheet.GetRow(13).GetCell(headColumnsIndex + 7).SetCellValue(head.AlCase);
                    double capmin, capmax;
                    capmin = Math.Round(head.Cap * (1 + head.CapMin / 100d), 2);
                    capmax = Math.Round(head.Cap * (1 + head.CapMax / 100d), 2);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 1).SetCellValue(capmin);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 3).SetCellValue(capmax);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 9).SetCellValue(head.CapRate);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 24).SetCellValue(head.RippleCurrent);
                    sheet.GetRow(5).GetCell(headColumnsIndex + 24).SetCellValue(
                        (int)Math.Ceiling(head.Voltage * 1.15));

                    sheet.GetRow(12).GetCell(headColumnsIndex + 13).SetCellValue(
                        capmin + "~" + capmax);
                    sheet.GetRow(12).GetCell(headColumnsIndex + 14).SetCellValue(head.DfMax);
                    
                    sheet.GetRow(12).GetCell(headColumnsIndex + 18).SetCellValue(head.LcMax);
                    sheet.GetRow(13).GetCell(headColumnsIndex + 14).SetCellValue(head.DfMaxAfter);
                    if (head.EsrMax > 0)
                    {
                        sheet.GetRow(12).GetCell(headColumnsIndex + 15).SetCellValue(head.EsrMax/1000);
                        sheet.GetRow(13).GetCell(headColumnsIndex + 15).SetCellValue(head.EsrMaxAfter/1000);
                        sheet.GetRow(13).GetCell(headColumnsIndex + 15).SetCellValue(head.EsrMaxAfter/1000);
                    }
                    else
                    {
                        if (head.ZMax > 0)
                        {
                            sheet.GetRow(12).GetCell(headColumnsIndex + 15).SetCellValue(head.ZMax/1000);
                            sheet.GetRow(13).GetCell(headColumnsIndex + 15).SetCellValue(head.ZMax/1000);
                            sheet.GetRow(13).GetCell(headColumnsIndex + 15).SetCellValue(head.ZMax/1000);
                        }
                    }

                    sheet.GetRow(13).GetCell(headColumnsIndex + 17).SetCellValue(
                        "初始值±" + head.CapRate + "%");
                    sheet.GetRow(13).GetCell(headColumnsIndex + 18).SetCellValue(head.LcMaxAfter);

                    sheet.GetRow(14).GetCell(headColumnsIndex + 13).SetCellValue(head.ElectricQty + " PCS");

                    if (head.IsValid)
                    {
                        sheet.GetRow(50).GetCell(1).SetCellValue("判定合格  " + head.Remark);
                    }
                    else
                    {
                        sheet.GetRow(50).GetCell(1).SetCellValue("判定不合格  " + head.Remark);
                    }

                    //Tested by:王燕
                    sheet.GetRow(53).GetCell(headColumnsIndex + 22).SetCellValue(head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }



                headColumnsIndex += 29;
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 19;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 17;
            int dtlColumns = 5;
            int dtlRowCount = 8;

            bool isApp = true;
            bool isCAP = true;
            bool isDF = true;
            bool isLC = true;
            bool isESR = true;
            bool isCAPRATE = true;

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i > 5) break;

                try
                {
                    if (i > 0 && !string.IsNullOrWhiteSpace(head.Dtls[i].Condiction))
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex).SetCellValue(
                            head.Dtls[i].Condiction
                            );
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }

                #region data
                int cIndex = currentDataColumnIndex;
                var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                if (caps.Any())
                {
                    var carpss = caps.ToList();
                    if(carpss.Where(T=>T.Valid=="2" && T.Value >0).Any())
                    {
                        isCAP = false;
                        isCAPRATE = false;
                    }
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                    //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                    for (int k = 0; k < carpss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (carpss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                cIndex = currentDataColumnIndex + 1;
                var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                if (dfs.Any())
                {
                    var dfss = dfs.ToList();
                    if (dfss.Where(T => T.Valid == "2").Any())
                    {
                        isDF = false;
                    }
                    for (int k = 0; k < dfss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (dfss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(dfss[k].Value);
                    }
                }

                if (i == 0)
                    cIndex = currentDataColumnIndex + 3;
                else
                    cIndex = currentDataColumnIndex + 4;
                var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                if (lcs.Any())
                {
                    var lcss = lcs.ToList();
                    if (lcss.Where(T => T.Valid == "2").Any())
                    {
                        isLC = false;
                    }
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                    //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                    for (int k = 0; k < lcss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (lcss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }


                cIndex = currentDataColumnIndex + 2;
                var zs = head.Dtls[i].Datas.Where(d => d.ItemCode == "ESR" && d.Value > 0);
                if (zs.Any())
                {
                    var zss = head.Dtls[i].Datas.Where(d => d.ItemCode == "ESR").ToList();
                    if (zss.Where(T => T.Valid == "2").Any())
                    {
                        isESR = false;
                    }
                    for (int k = 0; k < zss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (zss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(zss[k].Value / 1000);
                    }
                }
                else
                {
                    zs = head.Dtls[i].Datas.Where(d => d.ItemCode == "Z" && d.Value > 0);
                    if (zs.Any())
                    {
                        var zss = head.Dtls[i].Datas.Where(d => d.ItemCode == "Z").ToList();
                        if (zss.Where(T => T.Valid == "2").Any())
                        {
                            isESR = false;
                        }
                        for (int k = 0; k < zss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (zss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(zss[k].Value / 1000);
                        }
                    }
                }


                if (head.Dtls[i].Datas.Where(d => d.ItemCode == "L1"
                    && (d.Valid =="NG" || d.Valid == "N")).Any())
                {
                    isApp = false;
                }
                #endregion data

                currentDtlColumnIndex += dtlColumns;
                if (i == 0) currentDtlColumnIndex--;
                currentDataColumnIndex += dtlColumns;
                if (i == 0) currentDataColumnIndex--;

                if (i > 0 && (i + 1) % 6 == 0)
                {
                    currentDtlColumnIndex = 1;
                    currentDataColumnIndex = 1;
                    dataRowIndex += dtlRowCount;
                    currentHeadRowIndex += dtlRowCount;
                }

            }

            if(!isApp)
                sheet.GetRow(9).GetCell(22).SetCellValue("不合格");
            if (!isCAP)
                sheet.GetRow(10).GetCell(22).SetCellValue("不合格");
            if (!isDF)
                sheet.GetRow(11).GetCell(22).SetCellValue("不合格");
            if (!isLC)
                sheet.GetRow(12).GetCell(22).SetCellValue("不合格");
            if (!isESR)
                sheet.GetRow(13).GetCell(22).SetCellValue("不合格");
            if (!isCAPRATE)
                sheet.GetRow(14).GetCell(22).SetCellValue("不合格");
            #endregion

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
